/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.command;

import org.eclipse.gef.commands.Command;

import ro.pub.cs.se.moondance.directedit.ForbiddenIdentifierNames;
import ro.pub.cs.se.moondance.model.ClassVariable;
import ro.pub.cs.se.moondance.model.VisibilityType;

/**
 * Command to change the name and type of an attribute
 */
public class ChangeAttributeNameTypeCommand extends Command {

	private ClassVariable attribute;
	private String name, type, visibility, initialValue;
	private int multiplicity;
	private boolean isStatic;
	private String oldName, oldType, oldVisibility, oldInitialValue;
	private int oldMultiplicity;
	private boolean oldIsStatic;

	public ChangeAttributeNameTypeCommand(ClassVariable attribute) {
		this.attribute = attribute;
	}

	/**
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		attribute.setName(name);
		attribute.setType(type);
		attribute.setVisibility(visibility);
		attribute.setStatic(isStatic);
		attribute.setMultiplicity(multiplicity);
		attribute.setInitialValue(initialValue);
	}

	/**
	 * @return whether we can apply changes
	 */
	public boolean canExecute() {
		if (name != null && type != null && visibility != null)
			return true;
		else {
			name = oldName;
			type = oldType;
			visibility = oldVisibility;
			isStatic = oldIsStatic;
			multiplicity = oldMultiplicity;
			initialValue = oldInitialValue;
			return false;
		}
	}

	/**
	 * Sets the new Column name
	 * 
	 * @param string
	 *            the new name
	 */
	public void setConfigurationString(String string) {
		name = null;
		type = null;
		visibility = null;
		initialValue = null;
		isStatic = false;
		multiplicity = -1;
		if ((string != null) && (string.length() > 0)) {
			int colonIndex = string.indexOf(':');
			if (colonIndex >= 0) {
				String firstPart = string.substring(0, colonIndex);
				String secondPart = string.substring(colonIndex + 1);
				String[] result = firstPart.split("\\s"); // visiblity
				// <<static>> name

				switch (result.length) {
				case 1:// name
					if (!ForbiddenIdentifierNames.contains(result[0])
							&& (!(result[0].startsWith("<<") && result[0]
									.endsWith(">>")))) {
						name = result[0];
						visibility = VisibilityType.PUBLIC;
						isStatic = false;
					}
					break;
				case 2: // visibility name || pattern name
					if (result[0].startsWith("<<") && result[0].endsWith(">>")) {// pattern
						// name
						String pattern = result[0].substring(2, result[0]
								.length() - 2);
						if ((pattern.equals("static"))
								&& (!ForbiddenIdentifierNames
										.contains(result[1]))) {
							name = result[1];
							isStatic = true;
							visibility = VisibilityType.PUBLIC;
						}
					} else {// visibility name
						if ((VisibilityType.contains(result[0]))
								&& (!(result[1].startsWith("<<") && result[1]
										.endsWith(">>")))
								&& (!ForbiddenIdentifierNames
										.contains(result[1]))) {
							visibility = result[0];
							name = result[1];
							isStatic = false;
						}
					}
					break;
				case 3: // visibility pattern name
					if (VisibilityType.contains(result[0])) {
						String pattern = result[1].substring(2, result[1]
								.length() - 2);
						if (pattern.equals("static")
								&& (!ForbiddenIdentifierNames
										.contains(result[2]))) {
							visibility = result[0];
							isStatic = true;
							name = result[2];
						}
					}
					break;
				}

				int equalIndex = secondPart.indexOf('=');
				if (equalIndex > 0) {
					initialValue = secondPart.substring(equalIndex + 1);
					if (initialValue.length() == 0) {
						return;
					}
					secondPart = secondPart.substring(0, equalIndex);
				}

				int openedSquareBracketIndex = secondPart.indexOf('[');
				if (openedSquareBracketIndex > 0) {
					int closedSquareBracketIndex = secondPart.indexOf(']');
					String multiplicityString = secondPart.substring(
							openedSquareBracketIndex + 1,
							closedSquareBracketIndex);
					try {
						multiplicity = Integer.parseInt(multiplicityString);
					} catch (NumberFormatException nfe) {
						return;
					}

					secondPart = secondPart.substring(0,
							openedSquareBracketIndex);
				}
				if (secondPart.length() > 0) {// attribute has no type
					type = secondPart;
				}
			}
		}
	}

	/**
	 * Methods to set the old attribute values
	 */
	public void setOldName(String string) {
		oldName = string;
	}

	public void setOldType(String string) {
		oldType = string;
	}

	public void setOldVisibility(String string) {
		oldVisibility = string;
	}

	public void setOldInitialValue(String string) {
		oldInitialValue = string;
	}

	public void setOldStatic(boolean isStatic) {
		oldIsStatic = isStatic;
	}

	public void setOldMultiplicity(int multiplicity) {
		oldMultiplicity = multiplicity;
	}

	/**
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		attribute.setName(oldName);
		attribute.setType(oldType);
		attribute.setVisibility(oldVisibility);
		attribute.setStatic(oldIsStatic);
		attribute.setMultiplicity(oldMultiplicity);
		attribute.setInitialValue(oldInitialValue);
	}

}